package com.emmmya.ocs.modules.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.emmmya.ocs.common.entity.LayUITree;
import com.emmmya.ocs.common.utils.ResultUtil;
import com.emmmya.ocs.common.vo.Result;
import com.emmmya.ocs.modules.system.annotation.OperateLog;
import com.emmmya.ocs.modules.system.entity.Dict;
import com.emmmya.ocs.modules.system.entity.DictData;
import com.emmmya.ocs.modules.system.service.DictDataService;
import com.emmmya.ocs.modules.system.service.DictService;
import com.emmmya.ocs.modules.system.utils.LayuiTreeUtils;
import com.emmmya.ocs.modules.system.vo.DictDataVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: 字典与字典数据控制层
 * @author: 咕噜肉IT小店-黑医
 * @createDate: 2021-02-20
 * @version: 1.0
 */
@RequestMapping("system")
@Controller
@ResponseBody
@Api(tags = "字典模块")
public class DictController {

    @Autowired
    private DictService dictService;

    @Autowired
    private DictDataService dictDataService;

    @Autowired
    private LayuiTreeUtils layuiTreeUtils;

    @PostMapping("/getDictList")
    @ApiOperation(value = "查询所有字典数据", httpMethod = "POST", response = Result.class, notes = "")
    public Result<Object> getDictList(){
        List<Dict> dictList = dictService.getAll();
        List<LayUITree> layUITrees = layuiTreeUtils.changeDictTree(dictList, false,false);
        return ResultUtil.data(layUITrees);
    }

    @PostMapping("/addDict")
    @OperateLog(value="新增字典",logType = 1,operateType = 2,module = "字典管理模板")
    @ApiOperation(value = "新增字典", httpMethod = "POST", response = Result.class, notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(required = true, name = "dict", value = "字典对象",type="Dict", dataTypeClass = Dict.class)
    })
    public Result<Object> addDict(@RequestBody Dict dict){
        try {
            Dict save = dictService.save(dict);
            return ResultUtil.data(save,"新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("发生错误，请联系管理员。错误信息:"+e.getMessage());
        }
    }

    @RequestMapping("/updateDict")
    @OperateLog(value="修改字典",logType = 1,operateType = 3,module = "字典管理模板")
    public Result<Object> updateDict(@RequestBody Dict dict){
        try {
            Dict update = dictService.update(dict);
            return ResultUtil.data(update,"更新成功");
        } catch (Exception e) {
            //e.printStackTrace();
            return ResultUtil.error("发生错误，请联系管理员。错误信息:"+e.getMessage());
        }
    }

    @RequestMapping(value="/deleteDicts",method = RequestMethod.DELETE)
    @OperateLog(value="删除字典",logType = 1,operateType = 4,module = "字典管理模板")
    public Result<Object> deleteDicts(@RequestParam("ids[]")Long[] ids){
        try {
            if(ids!= null && ids.length != 0){
                for (Long id : ids) {
                    dictService.delete(id);
                }
            }
            return ResultUtil.success("删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("删除出错，如重复多次报错，请联系管理员。错误信息:"+e.getMessage());
        }
    }

    /**
     * 新增字典数据
     */
    @RequestMapping("/addDictData")
    @OperateLog(value="新增字典数据",logType = 1,operateType = 2,module = "字典管理模板")
    public Result<Object> addDictData(@RequestBody DictData dictData){
        try {
            DictData save = dictDataService.save(dictData);
            return ResultUtil.data(save,"保存成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("系统出错，错误信息："+e.getMessage());
        }
    }

    /**
     * 更新字典数据
     */
    @RequestMapping("/updateDictData")
    @OperateLog(value="修改字典数据",logType = 1,operateType = 3,module = "字典管理模板")
    public Result<Object> updateDictData(@RequestBody DictData dictData){
        try {
            DictData update = dictDataService.update(dictData);
            return ResultUtil.data(update,"保存成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("系统错误，错误信息："+e.getMessage());
        }
    }

    /**
     * 删除字典数据
     */
    @RequestMapping("/deleteDictData")
    @OperateLog(value="删除字典数据",logType = 1,operateType = 4,module = "字典管理模板",isSaveParam = true)
    public Result<Object> deleteDictData(@RequestBody() List<DictData> dictDatas){
        try {
            if(!dictDatas.isEmpty()) {
                for (DictData dictData : dictDatas) {
                    dictDataService.delete(dictData);
                }
            }
            return ResultUtil.success("删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("删除失败！请重试，如多次提示此信息，请联系管理员");
        }
    }

    /**
     * 根据查询条件来查询DictData
     */
    @RequestMapping(value="/getDictDataByVo",method = RequestMethod.GET)
    public Result<Object> getDictDataByVo(DictDataVo vo){
        try {
            IPage<DictData> page = vo.createPage();
            dictService.getDataListByPage(page,vo.createWrapper());
            return ResultUtil.data(page);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("系统错误，请先联系系统管理员！错误信息:"+e.getMessage());
        }
    }

    /**
     * 读取该字典下的字典数据。
     * @param parentId
     * @return
     */
    @RequestMapping(value="/getDictsByParentId",method = RequestMethod.GET)
    public Result<Object> getDictsByParentId(@RequestParam("parentId") Long parentId){
        try {
            if(parentId!= null && parentId.compareTo(0L) > 0){
                List<DictData> byParentId = dictService.findByParentId(parentId);
                return ResultUtil.data(byParentId);
            }else{
                return ResultUtil.error("传入参数有误，请确认参数！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("删除出错，如重复多次报错，请联系管理员。错误信息:"+e.getMessage());
        }
    }
}
